﻿using NHibernate;
using NHibernate.Cfg;

namespace Deepcode.QueryPatterns.Redux.Infra
{
	public sealed class NHibernateHelper
	{
		private static readonly ISessionFactory SessionFactory;
		private static ISession _currentSession;

		static NHibernateHelper()
		{
			SessionFactory = new Configuration().Configure().BuildSessionFactory();
		}

		public static ISession GetSession()
		{
			if (_currentSession == null)
			{
				_currentSession = SessionFactory.OpenSession();
			}

			return _currentSession;
		}

		public static void CloseSession()
		{
			if (_currentSession == null)
				return;

			_currentSession.Close();
			_currentSession = null;
		}

		public static void CloseSessionFactory()
		{
			if (SessionFactory != null)
			{
				SessionFactory.Close();
			}
		}
	}
}
